package com.asia5b.a5mall.home.adapter

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.asia5b.a5mall.home.R
import com.asia5b.a5mall.home.databinding.*
import com.asia5b.a5mall.home.listener.HomeAdapterListener
import com.asia5b.a5mall.home.widget.HomeGoodsItemView
import com.asia5b.a5mall.middleware.base.LIFE_CYCLE_PAUSE
import com.asia5b.a5mall.middleware.base.LIFE_CYCLE_RESUME
import com.asia5b.a5mall.middleware.base.SimpleLifecycleObserver
import com.asia5b.a5mall.middleware.model.bean.HomeMultiItemEntity
import com.asia5b.a5mall.middleware.model.home.*
import com.asia5b.a5mall.middleware.utils.dddLog
import com.asia5b.a5mall.middleware.utils.glide.BannerImageImlAdapter
import com.asia5b.a5mall.middleware.utils.then
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.youth.banner.Banner
import com.youth.banner.indicator.CircleIndicator
import java.lang.ref.WeakReference

/**
 * Created by zhangyuncai on 2019/12/6.
 */
class HomeAdapter(
        val simpleLifecycleObserver: SimpleLifecycleObserver,
        var list: ArrayList<HomeMultiItemEntity>,
        val mListener: HomeAdapterListener
) : BaseMultiItemQuickAdapter<HomeMultiItemEntity, BaseDataBindingHolder<*>>(list) {

    var banner1: WeakReference<Banner<*, *>>? = null
    var banner2: WeakReference<Banner<*, *>>? = null

    val lifyListener: (state: Int) -> Unit = {
        when (it) {
            LIFE_CYCLE_RESUME -> {
                dddLog("自动播放")
//                banner.start()
                banner1?.get()?.start()
                banner2?.get()?.start()
            }
            LIFE_CYCLE_PAUSE -> {
                dddLog("暂停自动播放")
//                banner.stop()
                banner1?.get()?.stop()
                banner2?.get()?.stop()
            }
        }
    }

    init {
        simpleLifecycleObserver.add(lifyListener)
        addItemType(HomeMultiItemEntity.FloorSingleImage, R.layout.item_home_floorsingleimage)
        addItemType(HomeMultiItemEntity.FloorLeftOneRightTwo, R.layout.item_home_lonertwoimage)
        addItemType(HomeMultiItemEntity.FloorRightOneLeftTwo, R.layout.item_home_ltworoneimage)
        addItemType(HomeMultiItemEntity.FloorThreeImage, R.layout.item_home_threeimage)
        addItemType(HomeMultiItemEntity.FloorFiveImage, R.layout.item_home_fiveimage)
        addItemType(HomeMultiItemEntity.FloorCornerBanner, R.layout.item_home_cornerbanner)
        addItemType(HomeMultiItemEntity.FloorFourImage, R.layout.item_home_fourimage)
        addItemType(HomeMultiItemEntity.FloorFourImage_31, R.layout.item_home_fourimage)
        addItemType(HomeMultiItemEntity.FloorTitle, R.layout.item_home_title)
        addItemType(HomeMultiItemEntity.FloorLeftRight, R.layout.item_home_leftright)
        addItemType(HomeMultiItemEntity.MemberRecommendGoods, R.layout.item_home2_goods)
        addItemType(HomeMultiItemEntity.FloorText, R.layout.item_home_text)
        addItemType(HomeMultiItemEntity.Banner, R.layout.item_home2_banner)
        addItemType(HomeMultiItemEntity.Menu, R.layout.item_home_menu)
    }


    override fun convert(helper: BaseDataBindingHolder<*>, item2: HomeMultiItemEntity) {
        helper.setIsRecyclable(false)
        if (item2 is HomeFloorItemModel) {
            val item = item2
            when (item.itemType) {
                HomeMultiItemEntity.FloorSingleImage -> {
                    (helper.dataBinding as ItemHomeFloorsingleimageBinding).apply {
                        this.listener = mListener
                        this.item = item.blockList?.getOrNull(0)
                    }
                }
                HomeMultiItemEntity.FloorLeftOneRightTwo -> {
                    (helper.dataBinding as ItemHomeLonertwoimageBinding).apply {
                        this.listener = mListener
                        this.itemA = item.blockList?.getOrNull(0)
                        this.itemB = item.blockList?.getOrNull(1)
                        this.itemC = item.blockList?.getOrNull(2)
                    }
                }
                HomeMultiItemEntity.FloorRightOneLeftTwo -> {
                    (helper.dataBinding as ItemHomeLtworoneimageBinding).apply {
                        this.listener = mListener
                        this.itemA = item.blockList?.getOrNull(0)
                        this.itemB = item.blockList?.getOrNull(1)
                        this.itemC = item.blockList?.getOrNull(2)
                    }
                }
                HomeMultiItemEntity.FloorThreeImage -> {
                    (helper.dataBinding as ItemHomeThreeimageBinding).apply {
                        this.listener = mListener
                        this.itemA = item.blockList?.getOrNull(0)
                        this.itemB = item.blockList?.getOrNull(1)
                        this.itemC = item.blockList?.getOrNull(2)
                    }
                }
                HomeMultiItemEntity.FloorFiveImage -> {
                    (helper.dataBinding as ItemHomeFiveimageBinding).apply {
                        this.listener = mListener
                        this.itemA = item.blockList?.getOrNull(0)
                        this.itemB = item.blockList?.getOrNull(1)
                        this.itemC = item.blockList?.getOrNull(2)
                        this.itemD = item.blockList?.getOrNull(3)
                        this.itemE = item.blockList?.getOrNull(4)
                    }
                }
                HomeMultiItemEntity.FloorCornerBanner -> {
                    setFloorCornerBannerView(helper, item)
                }
                HomeMultiItemEntity.FloorFourImage, HomeMultiItemEntity.FloorFourImage_31 -> {
                    (helper.dataBinding as ItemHomeFourimageBinding).apply {
                        this.listener = mListener
                        this.itemA = item.blockList?.getOrNull(0)
                        this.itemB = item.blockList?.getOrNull(1)
                        this.itemC = item.blockList?.getOrNull(2)
                        this.itemD = item.blockList?.getOrNull(3)
                    }
                }
                HomeMultiItemEntity.FloorTitle -> {
                    (helper.dataBinding as ItemHomeTitleBinding).apply {
                        this.listener = mListener
                        this.item = item.blockList?.getOrNull(0)
                    }
                }
                HomeMultiItemEntity.FloorLeftRight -> {
                    (helper.dataBinding as ItemHomeLeftrightBinding).apply {
                        this.listener = mListener
                        this.itemA = item.blockList?.getOrNull(0)
                        this.itemB = item.blockList?.getOrNull(1)
                        this.itemC = item.blockList?.getOrNull(2)
                    }
                }
                HomeMultiItemEntity.MemberRecommendGoods -> {
                    helper.setIsRecyclable(true)
                    setMemberRecommendGoodsView2(helper, item)
                }
                HomeMultiItemEntity.FloorText -> {
                    (helper.dataBinding as ItemHomeTextBinding).apply {
                        this.item = item.blockList?.getOrNull(0)
                    }
                }
            }
        } else if (item2 is HomeBannerContainerBean) {
            setHeaderBannerView(helper, item2)
        } else if (item2 is HomeMenuContainerBean) {
            setHeadMenuView(helper, item2)
        }

    }

    fun setHeadMenuView(helper: BaseViewHolder, item: HomeMenuContainerBean) {
        val menuList = item.homeMenuList!!
        val menuRecyclerView = helper.itemView as RecyclerView
        val menuAdapter = HomeMenuAdapter(menuList)
        menuRecyclerView.run {
            layoutManager = GridLayoutManager(context, 5)!!
            adapter = menuAdapter
        }
        menuAdapter.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position) as HomeMenuModel
            mListener.onClick(item)
        }
    }

    fun setFloorCornerBannerView(helper: BaseViewHolder, item: HomeFloorItemModel) {
        helper.setIsRecyclable(false)
        val list = item.toBannerList()
        val banner = helper.getView<Banner<*, *>>(R.id.banner_cornerbanner)
        initBannerView(list, banner)
        banner1 = WeakReference(banner)
        helper.itemView.visibility = if (list.isNullOrEmpty()) {
            View.GONE
        } else {
            View.VISIBLE
        }
    }

    /**
     * 初始化banner
     */
    fun initBannerView(item: ArrayList<HomeBlockBannerBean>, banner: Banner<*, *>) {
//        if (item.size == 1) {
//            item.addAll(item)
//        }
        banner.setLoopTime(2000)//设置动画时间
        banner.isAutoLoop(true)
        banner.adapter = BannerImageImlAdapter(item)
        banner.indicator = CircleIndicator(context)
        banner.start()


    }


    fun setHeaderBannerView(helper: BaseViewHolder, item: HomeBannerContainerBean) {
        helper.setIsRecyclable(false)

        val list = item.bannerList
        val banner = helper.getView<Banner<*, *>>(R.id.banner_home)
        banner2 = WeakReference(banner)
        list?.let {
            initHeadBannerView(list, banner)
        }
        helper.itemView.visibility = if (list.isNullOrEmpty()) {
            View.GONE
        } else {
            View.VISIBLE
        }

    }

    fun initHeadBannerView(item: ArrayList<HomeBannerMoel>, banner: Banner<*, *>) {
//        if (item.size == 1) {
//            item.addAll(item)
//        }
        banner.setLoopTime(2000)//设置动画时间
        banner.isAutoLoop(true)
        banner.adapter = BannerImageImlAdapter(item)
        banner.indicator = CircleIndicator(context)
        //点击事件
        banner.setOnBannerListener { data, position ->
            val model = item[position]
            mListener.onClick(model)
        }
        banner.start()
    }


    fun setMemberRecommendGoodsView2(helper: BaseViewHolder, floorViewModel: HomeFloorItemModel) {
        val goods1 = helper.getView<HomeGoodsItemView>(R.id.ll_home_goods1)
        val goods2 = helper.getView<HomeGoodsItemView>(R.id.ll_home_goods2)
        floorViewModel.blockList?.getOrNull(0)?.goods?.then {
            goods1.setData(it)
        }
        floorViewModel.blockList?.getOrNull(1)?.goods?.then {
            goods2.setData(it)
        }
    }

}